package cn.mysdp.biz.facade;


import cn.mysdp.biz.convert.*;
import cn.mysdp.biz.domain.*;
import cn.mysdp.biz.dto.request.*;
import cn.mysdp.biz.dto.response.*;
import cn.mysdp.biz.repository.*;
import cn.mysdp.biz.facade.*;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

/**
 * @ClassName: SdpWorkspaceFacadeBaseImpl
 * @Description:
 * @Author: SDP
 * @Date: 2021-10-30
 * @Version: 1.0
 * Table: sdp_workspace
 * Comment:
 * workspace
 */
public class SdpWorkspaceFacadeBaseImpl extends BaseServiceImpl implements SdpWorkspaceFacade {
    @Autowired
    protected SdpWorkspaceMapper sdpWorkspaceMapper;

    public static Map<String, String> uniqueFieldMap = initUniqueFieldMap();

    public static Map initUniqueFieldMap() {
        HashMap map = new HashMap<>();
        map.put("unique_workspace_index_5", "name");
        return map;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public Integer addSdpWorkspace(SdpWorkspaceAddRequest request) throws Exception {
        SdpWorkspaceWithBLOBs daoRequest = new SdpWorkspaceAddRequestConvert().convert(request);
        fixEqualCondition(request, "sdp_workspace", "add", "", null);
        Integer result = 0;
        try {
            result = sdpWorkspaceMapper.insertSelective(daoRequest);
        }catch(Exception ex) {
            ex.printStackTrace();
            throw checkDBDupException(ex, uniqueFieldMap);
        }
        if (result > 0) {
            result = daoRequest.getId();
        }
        addHistory(request, "A", "sdp_workspace", "api", result, null);
        return result;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public Integer updateSdpWorkspace(SdpWorkspaceUpdateRequest request) throws Exception {
        fixEqualCondition(request, "sdp_workspace", "update", request.getId()+"", null);
        checkInternalId(request, "sdp_workspace", "update", request.getId());
        addHistory(request, "U", "sdp_workspace", "api", request.getId(), null);

        SdpWorkspaceWithBLOBs daoRequest = new SdpWorkspaceUpdateRequestConvert().convert(request);
        SdpWorkspaceExample example = new SdpWorkspaceExample();
        example.createCriteria().andIdEqualTo(request.getId());
        if (request.getQueryOptions() != null && request.getQueryOptions().containsKey("setnull")) {
            example.setSetNull(JSONObject.toJavaObject(request.getQueryOptions().getJSONObject("setnull"), SdpWorkspaceExample.CriteriaString.class));
        }
        Integer result = 0;
        try {
            result = sdpWorkspaceMapper.updateByExampleSelective(daoRequest, example);
        }catch(Exception ex) {
            ex.printStackTrace();
            throw checkDBDupException(ex, uniqueFieldMap);
        }
        if (result > 0) {
            return result;
        }
        throw createUpdateFailException();
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public Integer deleteSdpWorkspace(SdpWorkspaceDeleteRequest request) throws Exception {
        Date now = new Date();
        Integer count = 0;

        BaseRequest abstractRequest = new BaseRequest();
        copyProperties(request, abstractRequest);

        for(int index = 0; index < request.getIdList().size(); index++) {
            fixEqualCondition(request, "sdp_workspace", "delete", request.getIdList().get(index)+"", null);
            checkInternalId(request, "sdp_workspace", "delete", request.getIdList().get(index));
            addHistory(abstractRequest, "D", "sdp_workspace", "api", request.getIdList().get(index), null);
            sdpWorkspaceMapper.deleteByPrimaryKey(request.getIdList().get(index));
            count++;
        }
        return count;
    }

    @Override
    public SdpWorkspaceQueryResponse querySdpWorkspace(SdpWorkspaceQueryRequest request) throws Exception {
        SdpWorkspaceWithBLOBs daoResponse = sdpWorkspaceMapper.selectByPrimaryKeyWithBLOBs(request.getId());
        SdpWorkspaceQueryResponse response = new SdpWorkspaceQueryResponse();
        copyProperties(daoResponse, response);
        return response;
    }

    @Override
    public List<SdpWorkspaceQueryResponse> listSdpWorkspace(SdpWorkspaceQueryRequest request) throws Exception {
        SdpWorkspaceWithBLOBs daoRequest = new SdpWorkspaceWithBLOBs();
        copyProperties(request, daoRequest);
        SdpWorkspaceExample example;
        try {
            example = SdpWorkspaceExample.createExample(daoRequest, request.getQueryOptions());
        } catch (Exception ex) {
            ex.printStackTrace();
            throw createFailException(ex);
        }

        List<SdpWorkspaceWithBLOBs> list;
        try {
            list = sdpWorkspaceMapper.selectByExampleWithBLOBs(example);
        } catch (Exception ex) {
            ex.printStackTrace();
            throw createDatabaseFailException(ex);
        }

        List<SdpWorkspaceQueryResponse> responseList = new ArrayList<>();
        for(SdpWorkspaceWithBLOBs item : list) {
            SdpWorkspaceQueryResponse newItem = new SdpWorkspaceQueryResponse();
            copyProperties(item, newItem);
            responseList.add(newItem);
        }

        return responseList;
    }

    @Override
    public Integer countSdpWorkspace(SdpWorkspaceQueryRequest request) throws Exception {
        SdpWorkspaceWithBLOBs daoRequest = new SdpWorkspaceWithBLOBs();
        copyProperties(request, daoRequest);
        SdpWorkspaceExample example;
        try {
            example = SdpWorkspaceExample.createExample(daoRequest, request.getQueryOptions());
        } catch (Exception ex) {
            ex.printStackTrace();
            throw createFailException(ex);
        }

        Integer count;
        try {
            count = sdpWorkspaceMapper.countByExample(example);
        } catch (Exception ex) {
            ex.printStackTrace();
            throw createDatabaseFailException(ex);
        }

        return count;
    }

    @Override
    public BaseStatResponse statSdpWorkspace(SdpWorkspaceQueryRequest request) throws Exception {
        throw createFailException("not implement");
    }

    @Override
    public List<SdpWorkspaceForGetColumnListResponse> getColumnList(SdpWorkspaceForGetColumnListRequest request) throws Exception {
        SdpWorkspaceForGetColumnList daoRequest = new SdpWorkspaceForGetColumnList();
        copyProperties(request, daoRequest);
        
        List<SdpWorkspaceForGetColumnList> list;
        
        SdpWorkspaceForGetColumnListExample example;
        try {
            example = SdpWorkspaceForGetColumnListExample.createExample(daoRequest, request.getQueryOptions());
        } catch (Exception ex) {
            ex.printStackTrace();
            throw createFailException(ex);
        }
        
        try {
            list = sdpWorkspaceMapper.getColumnListByExample(example);
        } catch (Exception ex) {
            ex.printStackTrace();
            throw createDatabaseFailException(ex);
        }
        
        List<SdpWorkspaceForGetColumnListResponse> responseList = new ArrayList<>();
        for(SdpWorkspaceForGetColumnList item : list) {
            SdpWorkspaceForGetColumnListResponse newItem = new SdpWorkspaceForGetColumnListResponse();
            copyProperties(item, newItem);
            responseList.add(newItem);
        }
        
        return responseList;
    }

    @Override
    public Integer countForGetColumnList(SdpWorkspaceForGetColumnListRequest request) throws Exception {
        SdpWorkspaceForGetColumnList daoRequest = new SdpWorkspaceForGetColumnList();
        copyProperties(request, daoRequest);
        
        Integer count;
        
        SdpWorkspaceForGetColumnListExample example;
        try {
            example = SdpWorkspaceForGetColumnListExample.createExample(daoRequest, request.getQueryOptions());
        } catch (Exception ex) {
            ex.printStackTrace();
            throw createFailException(ex);
        }
        
        try {
            count = sdpWorkspaceMapper.countGetColumnListByExample(example);
        } catch (Exception ex) {
            ex.printStackTrace();
            throw createDatabaseFailException(ex);
        }
        
        return count;
    }
    
    @Override
    public BaseStatResponse statForGetColumnList(SdpWorkspaceForGetColumnListRequest request) throws Exception {
        throw createFailException("not implement");
    }

    @Override
    public List<SdpWorkspaceForGetTableListResponse> getTableList(SdpWorkspaceForGetTableListRequest request) throws Exception {
        SdpWorkspaceForGetTableList daoRequest = new SdpWorkspaceForGetTableList();
        copyProperties(request, daoRequest);
        
        List<SdpWorkspaceForGetTableList> list;
        
        SdpWorkspaceForGetTableListExample example;
        try {
            example = SdpWorkspaceForGetTableListExample.createExample(daoRequest, request.getQueryOptions());
        } catch (Exception ex) {
            ex.printStackTrace();
            throw createFailException(ex);
        }
        
        try {
            list = sdpWorkspaceMapper.getTableListByExample(example);
        } catch (Exception ex) {
            ex.printStackTrace();
            throw createDatabaseFailException(ex);
        }
        
        List<SdpWorkspaceForGetTableListResponse> responseList = new ArrayList<>();
        for(SdpWorkspaceForGetTableList item : list) {
            SdpWorkspaceForGetTableListResponse newItem = new SdpWorkspaceForGetTableListResponse();
            copyProperties(item, newItem);
            responseList.add(newItem);
        }
        
        return responseList;
    }

    @Override
    public Integer countForGetTableList(SdpWorkspaceForGetTableListRequest request) throws Exception {
        SdpWorkspaceForGetTableList daoRequest = new SdpWorkspaceForGetTableList();
        copyProperties(request, daoRequest);
        
        Integer count;
        
        SdpWorkspaceForGetTableListExample example;
        try {
            example = SdpWorkspaceForGetTableListExample.createExample(daoRequest, request.getQueryOptions());
        } catch (Exception ex) {
            ex.printStackTrace();
            throw createFailException(ex);
        }
        
        try {
            count = sdpWorkspaceMapper.countGetTableListByExample(example);
        } catch (Exception ex) {
            ex.printStackTrace();
            throw createDatabaseFailException(ex);
        }
        
        return count;
    }
    
    @Override
    public BaseStatResponse statForGetTableList(SdpWorkspaceForGetTableListRequest request) throws Exception {
        throw createFailException("not implement");
    }

    @Override
    public List<SdpWorkspaceForGetDbConfigResponse> getDbConfig(SdpWorkspaceForGetDbConfigRequest request) throws Exception {
        SdpWorkspaceForGetDbConfig daoRequest = new SdpWorkspaceForGetDbConfig();
        copyProperties(request, daoRequest);
        
        List<SdpWorkspaceForGetDbConfig> list;
        
        SdpWorkspaceForGetDbConfigExample example;
        try {
            example = SdpWorkspaceForGetDbConfigExample.createExample(daoRequest, request.getQueryOptions());
        } catch (Exception ex) {
            ex.printStackTrace();
            throw createFailException(ex);
        }
        
        try {
            list = sdpWorkspaceMapper.getDbConfigByExample(example);
        } catch (Exception ex) {
            ex.printStackTrace();
            throw createDatabaseFailException(ex);
        }
        
        List<SdpWorkspaceForGetDbConfigResponse> responseList = new ArrayList<>();
        for(SdpWorkspaceForGetDbConfig item : list) {
            SdpWorkspaceForGetDbConfigResponse newItem = new SdpWorkspaceForGetDbConfigResponse();
            copyProperties(item, newItem);
            responseList.add(newItem);
        }
        
        return responseList;
    }

    @Override
    public Integer countForGetDbConfig(SdpWorkspaceForGetDbConfigRequest request) throws Exception {
        SdpWorkspaceForGetDbConfig daoRequest = new SdpWorkspaceForGetDbConfig();
        copyProperties(request, daoRequest);
        
        Integer count;
        
        SdpWorkspaceForGetDbConfigExample example;
        try {
            example = SdpWorkspaceForGetDbConfigExample.createExample(daoRequest, request.getQueryOptions());
        } catch (Exception ex) {
            ex.printStackTrace();
            throw createFailException(ex);
        }
        
        try {
            count = sdpWorkspaceMapper.countGetDbConfigByExample(example);
        } catch (Exception ex) {
            ex.printStackTrace();
            throw createDatabaseFailException(ex);
        }
        
        return count;
    }
    
    @Override
    public BaseStatResponse statForGetDbConfig(SdpWorkspaceForGetDbConfigRequest request) throws Exception {
        throw createFailException("not implement");
    }

    @Override
    public Integer clone(BaseNameIdRequest request) throws Exception {
        throw createFailException("not implement");
    }

    @Override
    public Integer cloneWorkspace(BaseNameIdRequest request) throws Exception {
        throw createFailException("not implement");
    }

    @Override
    public String fixJsonObject(BaseNameRequest request) throws Exception {
        throw createFailException("not implement");
    }

    @Override
    public String testConnect(SdpWorkspaceUpdateRequest request) throws Exception {
        throw createFailException("not implement");
    }

}
